<?php

namespace app\admin\controller;
use Qiniu\Auth as Auth;
use Qiniu\Storage\UploadManager;
//分类管理
class Type extends Common
{
    //分类列表
    public function type_list()
    {
        $where = array('a.gt_id' => array('gt', 0));
        $name = Request()->param('name');
        //名称
        $status = Request()->param('status');
        //状态
        $start_time = strtotime(Request()->param('start_time'));
        $end_time = strtotime(Request()->param('end_time'));
        $page = Request()->param('page');
        $size = Request()->param('size');
        if ($page < 1) {
            $page = 1;
        }
        if ($size < 15) {
            $size = 15;
        }
        $now = ($page - 1) * $size;
        if ($name != '') {
            $where['a.gt_name'] = array('like', '%' . $name . '%');
        }
        if ($status > 0) {
            if ($status == 1) {
                $where['a.gt_status'] = 1;
            } else {
                $where['a.gt_status'] = 2;
            }

        }
        $where_time = array(array('gt', 0), array('gt', 0));
        if ($start_time > 0) {
            $where_time[] = array('egt', $start_time);
        } else {
            $start_time = '';
        }
        if ($end_time > 0) {
            $where_time[] = array('elt', $end_time);
        } else {
            $end_time = '';
        }
        $where['a.gt_create_time'] = $where_time;
        $type = db('goods_type a')
            ->field("a.*,c.gt_name as gt_p_name,(select count(b.g_id) from fr_goods b where b.gt_id1=a.gt_id or b.gt_id2=a.gt_id or b.gt_id=a.gt_id) as g_number")
            ->join('fr_goods_type c', 'a.gt_pid = c.gt_id', 'left')
            ->order('a.gt_sort desc,a.gt_status')
            ->where($where)
            ->limit("$now,$size")
            ->select();
        //标签列表
        $count = db('goods_type a')
            ->where($where)
            ->count();
        //等级信息
        $page_str = page($page, $size, $count, count($type));//分页
        $this->view->assign('page_str', $page_str);
        $this->view->assign('data', $type);
        $this->assign('status', $status);
        $this->assign('name', $name);
        $this->assign('start_time', $start_time);
        $this->assign('end_time', $end_time);
        $this->assign('page', $page);
        $this->assign('size', $size);
        $this->assign('count', $count);
        return $this->view->fetch();

    }

    //分类列表
    public function type_list_other()
    {
        $where = array('a.gt_id' => array('gt', 0));
        $name = Request()->param('name');
        //名称
        $status = Request()->param('status');
        //状态
        $start_time = strtotime(Request()->param('start_time'));
        $end_time = strtotime(Request()->param('end_time'));
        if ($name != '') {
            $where['a.gt_name'] = array('like', '%' . $name . '%');
        }
        if ($status > 0) {
            if ($status == 1) {
                $where['a.gt_status'] = 1;
            } else {
                $where['a.gt_status'] = 2;
            }

        }
        $where_time = array(array('gt', 0), array('gt', 0));
        if ($start_time > 0) {
            $where_time[] = array('egt', $start_time);
        } else {
            $start_time = '';
        }
        if ($end_time > 0) {
            $where_time[] = array('elt', $end_time);
        } else {
            $end_time = '';
        }
        $where['a.gt_create_time'] = $where_time;
        $type = db('goods_type a')
            ->field("a.*,c.gt_name as gt_p_name,(select count(b.g_id) from fr_goods b where b.gt_id1=a.gt_id or b.gt_id2=a.gt_id or b.gt_id=a.gt_id) as g_number")
            ->join('fr_goods_type c', 'a.gt_pid = c.gt_id', 'left')
            ->order('gt_type,a.gt_sort desc,a.gt_status')
            ->where($where)
            ->select();
        //标签列表
        $type1 = array();
        $type2 = array();
        $type3 = array();
        foreach ($type as $k => $v) {
            if ($v['gt_type'] == 1) {
                $type1[] = $v;
            }
            if ($v['gt_type'] == 2) {
                $type2[] = $v;
            }
            if ($v['gt_type'] == 3) {
                $type3[] = $v;
            }
        }
        $type = array();
        foreach ($type1 as $k => $v) {
            $type[] = $v;
            foreach ($type2 as $kk => $vv) {
                if ($vv['gt_pid'] == $v['gt_id']) {
                    $type[] = $vv;
                    foreach ($type3 as $kkk => $vvv) {
                        if ($vvv['gt_pid'] == $vv['gt_id']) {
                            $type[] = $vvv;
                        }
                    }
                }
            }
        }
        $count = count($type);
        //等级信息
        $this->view->assign('data', $type);
        $this->assign('status', $status);
        $this->assign('name', $name);
        $this->assign('start_time', $start_time);
        $this->assign('end_time', $end_time);
        $this->assign('count', $count);
        return $this->view->fetch();

    }

    //添加标签
    public function add_type()
    {
        if (request()->isGet()) {
            //get，加载页面
            $type = db('goods_type')
                ->where(array('gt_status' => 1, 'gt_type' => array('in', '1,2')))
                ->order("gt_sort desc,gt_type")
                ->select();
            $type1 = array();
            $type2 = array();
            foreach ($type as $k => $v) {
                if ($v['gt_type'] == 1) {
                    $type1[] = $v;
                }
                if ($v['gt_type'] == 2) {
                    $type2[] = $v;
                }
            }
            $this->assign('type1', $type1);
            $this->assign('type2', $type2);
            return $this->view->fetch();
        } else if (request()->isAjax()) {
            $data = request()->post();
            $data['gt_name'] = trim($data['gt_name']);
            if ($data['gt_name'] == '') {
                return return_json(array('code' => 0, 'msg' => '请输入分类名称'), '添加分类');
            }
            $pid = $data['gt_pid'];
            if ($pid == 0) {
                $data['gt_type'] = 1;
            } else {
                $p_type = db('goods_type')
                    ->where(array('gt_status' => 1, 'gt_id' => $pid))
                    ->find();
                if (empty($p_type)) {
                    return return_json(array('code' => 0, 'msg' => '没有该上级分类'), '添加分类');
                }
                if ($p_type['gt_type'] == 1) {
                    $data['gt_type'] = 2;
                } else if ($p_type['gt_type'] == 2) {
                    $data['gt_type'] = 3;
                }
            }
            if ($data['gt_sort'] <= 0) {
                $data['gt_sort'] = 0;
            }
            $type = db('goods_type')->where(array('gt_name' => $data['gt_name']))->find();
            if (!empty($type)) {
                return return_json(array('code' => 0, 'msg' => '已有相同名称的分类'), '添加分类');
            }
            if ($data['gt_status'] != 1) {
                $data['gt_end_time'] = time();
            }
            $data['gt_create_time'] = time();
            $data['gt_add_user_id'] = session('uid');
            $data['gt_add_user_name'] = session('user.real_name');
            $file = Request()->file("gt_img");
            if (!$file && $data['gt_type'] != 2) {
                return return_json(array('code' => 0, 'msg' => '请上传图片！'), '添加分类');
                // form_json(array('code'=>0,'msg'=>'请选择图片！'),'添加banner');
                // echo '<script type="text/javascript">alert("请选择图片");history.go(-1);</script>';die;
            }
            if ($file) {
//                $info = $file->move(ROOT_PATH.'public/upload/type'.DS);
//                $img="/upload/type".DS.$info->getSaveName();
                //以下为图片上传到其他地址(阿里云)
                $upload = new Upload();
                $return=$upload->upload($file);
                if($return['code']!=1){
                    return return_json(array('code'=>0,'msg'=>'图片上传失败:'.$return['msg']),'上传图片');
                }else{
                    $img=$return['url'];
                }
                //以上为图片上传到其他地址(阿里云)
                $data['gt_img'] = $img;
            }
            unset($data['uploadfile']);
            $res = db('goods_type')->insertGetId($data);
            //添加权限
            if ($res > 0) {
                return return_json(array('code' => 1, 'msg' => '分类添加成功'), '添加分类');
            } else {
                return return_json(array('code' => 0, 'msg' => '分类添加失败'), '添加分类');
            }
        }
    }

    //修改标签
    public function retype()
    {
        if (request()->isGet()) {
            //get，加载页面
            $id = request()->get('id');
            $t_type = db('goods_type')
                ->where(array('gt_id' => $id))
                ->find();
            $type = db('goods_type')
                ->where(array('gt_status' => 1, 'gt_type' => array('in', '1,2')))
                ->order("gt_sort desc,gt_type")
                ->select();
            $type1 = array();
            $type2 = array();
            foreach ($type as $k => $v) {
                if ($v['gt_type'] == 1) {
                    $type1[] = $v;
                }
                if ($v['gt_type'] == 2) {
                    $type2[] = $v;
                }
            }
            $this->assign('type', $t_type);
            $this->assign('type1', $type1);
            $this->assign('type2', $type2);
            return $this->view->fetch();
        } else if (request()->isAjax()) {
            $data = request()->post();
            $id = $data['id'];
            unset($data['id']);
            if ($data['gt_name'] == '') {
                return return_json(array('code' => 0, 'msg' => '请输入分类名称'), '修改分类');
            }
            $pid = $data['gt_pid'];
            if ($pid == 0) {
                $data['gt_type'] = 1;
            } else {
                $p_type = db('goods_type')
                    ->where(array('gt_status' => 1, 'gt_id' => $pid))
                    ->find();
                if (empty($p_type)) {
                    return return_json(array('code' => 0, 'msg' => '没有该上级分类'), '修改分类');
                }
                if ($p_type['gt_type'] == 1) {
                    $data['gt_type'] = 2;
                } else if ($p_type['gt_type'] == 2) {
                    $data['gt_type'] = 3;
                }
            }
            if ($data['gt_sort'] <= 0) {
                $data['gt_sort'] = 0;
            }
            $type = db('goods_type')->where(array('gt_name' => $data['gt_name'], 'gt_id' => array('neq', $id)))->find();
            if (!empty($type)) {
                return return_json(array('code' => 0, 'msg' => '已有相同名称的分类'), '修改分类');
            }
            if ($data['gt_status'] != 1 && $type['gt_status'] == 1) {
                $data['gt_end_time'] = time();
            }
            $data['gt_update_time'] = time();
            $file = Request()->file("gt_img");
            if ($file) {
//                $info = $file->move(ROOT_PATH.'public/upload/type'.DS);
////                $img="/upload/type".DS.$info->getSaveName();
                //以下为图片上传到其他地址(阿里云)
                $upload = new Upload();
                $return=$upload->upload($file);
                if($return['code']!=1){
                    return return_json(array('code'=>0,'msg'=>'图片上传失败:'.$return['msg']),'上传图片');
                }else{
                    $img=$return['url'];
                }
                //以上为图片上传到其他地址(阿里云)
                $data['gt_img'] = $img;
            }
            unset($data['uploadfile']);
            $res = db('goods_type')->where(array('gt_id' => $id))->update($data);
            //添加权限
            if ($res !== false) {
                return return_json(array('code' => 1, 'msg' => '分类修改成功'), '修改分类');
            } else {
                return return_json(array('code' => 0, 'msg' => '分类修改失败'), '修改分类');
            }
        }
    }

    //禁用分类
    function type_del()
    {
        if (request()->isAjax()) {
            $id = request()->post('id');
            $res = db('goods_type')
                ->where(array('gt_id' => $id))
                ->update(array('gt_status' => 2, 'gt_end_time' => time()));
            if ($res !== false) {
                return return_json(array('code' => 1, 'msg' => '禁用成功'), '禁用分类');
            } else {
                return return_json(array('code' => 0, 'msg' => '禁用失败'), '禁用分类');
            }
        }
    }

    //启用分类
    function type_use()
    {
        if (request()->isAjax()) {
            $id = request()->post('id');
            $res = db('goods_type')
                ->where(array('gt_id' => $id))
                ->update(array('gt_status' => 1, 'gt_end_time' => 0));
            if ($res !== false) {
                return return_json(array('code' => 1, 'msg' => '启用成功'), '启用分类');
            } else {
                return return_json(array('code' => 0, 'msg' => '启用失败'), '启用分类');
            }
        }
    }
    //添加分类
    public function add_type_csv(){
        if(request()->isGet()){
            return $this->view->fetch();
        }else if(request()->isAjax()){
            set_time_limit(0);
            $file=request()->file('csv_file');
            if(!$file){
                return return_json(array('code'=>0,'msg'=>'请选择文件'),'导入分类');
            }
            //以下将模板传入第三方
//            $upload = new Upload();
//            $return=$upload->upload($file);
//            if($return['code']!=1){
//                return return_json(array('code'=>0,'msg'=>'图片上传失败:'.$return['msg']),'上传模板');
//            }else{
//                $data['g_img']=$return['url'];
//            }
//            echo $data['g_img'];die;
            //http://sczxyx-mall.oss-cn-chengdu.aliyuncs.com/a5e6c20190821113542811737.csv
            //模板地址
            //以上将模板传入第三方
            $ext   = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀
            if($ext!='csv' && $ext!='CSV'){
                return return_json(array('code'=>0,'msg'=>'只支持csv文件'),'导入分类');
            }
            $csv_file = fopen($file->getInfo('tmp_name'), 'r');
            //打开文件
            while($data=fgetcsv($csv_file,1000,",")){
                $data = eval('return '.iconv('gbk','utf-8',var_export($data,true)).';');//防止乱码
                //读取有保单号的数据
                $list[] = $data;

            }
            //将文件内容读取未数组
            unset($list[0]);//去除第一行
            $system=db('system')->find();
            //系统设置
            $all_type=array();
            $type3=array();//第三级分类数组
            foreach($list as $v) {
                if($v[1]!=''){
                    //名称不为空
                    if($v[0]==1){
                        //一级分类
                        foreach($all_type as $vv){
                            //在已有数据中查询，减少数据库查询
                            if($vv['gt_type']==1 && $v[1]==$vv['gt_name']){
                                $type=$vv;
                            }
                        }
                        if(empty($type)){
                            $type=db('goods_type')
                                ->where(array('gt_name'=>$v[1],'gt_type'=>1))
                                ->find();
                            if(empty($type)){
                                $type=array('gt_name'=>$v[1],'gt_type'=>1,'gt_pid'=>0,'gt_sort'=>$v[5]>0?$v[5]:0,'gt_create_time'=>time(),'gt_status'=>$v[3]=='启用'?1:2,'gt_is_index'=>$v[4]=='首页展示'?1:2,'gt_add_user_id'=>session('uid'),'gt_add_user_name'=>session('user.real_name'),'gt_img'=>$system['type_img']);
                                $res=db('goods_type')->insertGetId($type);
                                $type['gt_id']=$res;
                                $all_type[]=$type;
                            }else{
                                $all_type[]=$type;
                            }
                        }
                        unset($type);
                    }else if($v[0]==2 && $v[2]!=''){
                        foreach($all_type as $vv){
                            //在已有数据中查询，减少数据库查询
                            if($vv['gt_type']==2 && $v[1]==$vv['gt_name'] && $v[2]==$vv['gt_pname']){
                                $type=$vv;
                            }
                            if($vv['gt_type']==1 && $v[2]==$vv['gt_name']){
                                $p_type=$vv;
                            }
                        }
                        if(empty($type)){
                            if(empty($p_type)){
                                $p_type=db('goods_type')
                                    ->where(array('gt_name'=>$v[2],'gt_type'=>1))
                                    ->find();
                                if(!empty($p_type)){
                                    $all_type[]=$p_type;
                                }
                            }
                            if(!empty($p_type)){
                                $type=db('goods_type')
                                    ->where(array('gt_name'=>$v[1],'gt_type'=>2))
                                    ->find();
                                if(empty($type)){
                                    $type=array('gt_name'=>$v[1],'gt_type'=>2,'gt_pid'=>$p_type['gt_id'],'gt_sort'=>$v[5]>0?$v[5]:0,'gt_create_time'=>time(),'gt_status'=>$v[3]=='启用'?1:2,'gt_is_index'=>$v[4]=='首页展示'?1:2,'gt_add_user_id'=>session('uid'),'gt_add_user_name'=>session('user.real_name'),'gt_img'=>$system['type_img']);
                                    $res=db('goods_type')->insertGetId($type);
                                    $type['gt_id']=$res;
                                    $type['gt_pname']=$p_type['gt_name'];
                                    $all_type[]=$type;
                                }else{
                                    $type['gt_pname']=$p_type['gt_name'];
                                    $all_type[]=$type;
                                }
                            }
                        }
                        unset($type,$p_type);
                    }else if($v[0]==3 && $v[2]!=''){
                        foreach($all_type as $vv){
                            //在已有数据中查询，减少数据库查询
                            if($vv['gt_type']==3 && $v[1]==$vv['gt_name'] && $v[2]==$vv['gt_pname']){
                                $type=$vv;
                            }
                            if($vv['gt_type']==2 && $v[2]==$vv['gt_name']){
                                $p_type=$vv;
                            }
                        }
                        if(empty($type)){
                            if(empty($p_type)) {
                                $p_type = db('goods_type')
                                    ->where(array('gt_name' => $v[2], 'gt_type' => 2))
                                    ->find();
                                if(!empty($p_type)){
                                    $all_type[]=$p_type;
                                }
                            }
                            if(!empty($p_type)){
                                $type=db('goods_type')
                                    ->where(array('gt_name'=>$v[1],'gt_type'=>3))
                                    ->find();
                                if(empty($type)){
                                    $type=array('gt_name'=>$v[1],'gt_type'=>3,'gt_pid'=>$p_type['gt_id'],'gt_sort'=>$v[5]>0?$v[5]:0,'gt_create_time'=>time(),'gt_status'=>$v[3]=='启用'?1:2,'gt_is_index'=>$v[4]=='首页展示'?1:2,'gt_add_user_id'=>session('uid'),'gt_add_user_name'=>session('user.real_name'),'gt_img'=>$system['type_img']);
//                                    $res=db('goods_type')->insertGetId($type);
//                                    $type['gt_id']=$res;
                                    $type3[]=$type;
                                    $type['gt_pname']=$p_type['gt_name'];
                                    $all_type[]=$type;
                                }else{
                                    $type['gt_pname']=$p_type['gt_name'];
                                    $all_type[]=$type;
                                }
                            }
                        }
                        unset($type,$p_type);
                    }
                }
            }
            if(!empty($type3) && count($type3)>0){
                db('goods_type')->insertAll($type3);
            }
            return return_json(array('code' => 1, 'msg' => '分类添加成功'), '导入分类');
        }
    }
}
